package manage.action;

import java.util.ArrayList;
import java.util.List;

import common.bean.Option;
import manage.model.CostType;
import manage.model.IncomeType;
import manage.service.CostTypeService;
import manage.service.IncomeTypeService;

import com.opensymphony.xwork2.ActionSupport;

/**
 * @author xuweigui
 *
 */
@SuppressWarnings("serial")
public class IncomeTypeAction extends ActionSupport {

    private List<IncomeType> incomeTypes = new ArrayList<IncomeType>();
    private IncomeTypeService incomeTypeService;
    private IncomeType incomeType;
    //for ajax call 
    //result="success" if successful, otherwise error message content
    private String result;

    private Integer parentId = null;
    private List<Option> options = new ArrayList<Option>();

    public String deleteIncomeType() {
          incomeTypeService.remove(incomeType.getId());
          return SUCCESS;
      }
    public String execute() {
        incomeTypes = incomeTypeService.findAll();
        return SUCCESS;
    }
    //get income types
    public String getIncomeTypeOption() {
        if (parentId == null) {
            incomeTypes = incomeTypeService.findAll();
        } else {
            incomeTypes = incomeTypeService.findChildren(parentId);
        }
        for(IncomeType incomeType : incomeTypes) {
            Option option = new Option();
            option.setId(incomeType.getId());
            option.setName(incomeType.getName());
            option.setParentId(incomeType.getParentId());
            options.add(option);
        }
        return SUCCESS;
    }
    
    public String createIncomeType() {
        incomeTypeService.add(incomeType);
        return SUCCESS;
    }

    public List<Option> getOptions() {
        return options;
    }

    public void setOptions(List<Option> options) {
        this.options = options;
    }

    public Integer getParentId() {
        return parentId;
    }

    public void setParentId(Integer parentId) {
        this.parentId = parentId;
    }

    public List<IncomeType> getIncomeTypes() {
        return incomeTypes;
    }
    public void setIncomeTypes(List<IncomeType> incomeTypes) {
        this.incomeTypes = incomeTypes;
    }
    public IncomeTypeService getIncomeTypeService() {
        return incomeTypeService;
    }
    public void setIncomeTypeService(IncomeTypeService incomeTypeService) {
        this.incomeTypeService = incomeTypeService;
    }
    public IncomeType getIncomeType() {
        return incomeType;
    }
    public void setIncomeType(IncomeType incomeType) {
        this.incomeType = incomeType;
    }
    public String getResult() {
        return result;
    }
    public void setResult(String result) {
        this.result = result;
    }

    
}
